package com.example.farmer.Adapter.Class;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.farmer.Database.ClassPostDatabase;
import com.example.farmer.Entity.ClassPost;
import com.example.farmer.R;
import com.example.farmer.Seller_Home.NewsDetailActivity;
import com.example.farmer.WebActivity.News;
import com.example.farmer.WebActivity.haibaoquan;

import java.util.List;

public class CircleNew_PostClassAdapter extends RecyclerView.Adapter<CircleNew_PostClassAdapter.PostViewHolder> {
    private static List<ClassPost> classPosts;
    private static Context context;

    // 构造函数，接收帖子列表和上下文
    public CircleNew_PostClassAdapter(List<ClassPost> classPosts, Context context) {
        this.context = context;
        this.classPosts = classPosts;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 从布局文件中加载帖子项的视图
        View postView = LayoutInflater.from(parent.getContext()).inflate(R.layout.class_post_item, parent, false);
        return new PostViewHolder(postView);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        // 获取当前位置的帖子对象
        ClassPost classPost = classPosts.get(position);
        // 绑定帖子内容和图片到ViewHolder
        holder.bind(classPost);
    }

    @Override
    public int getItemCount() {
        // 返回帖子列表的大小
        return classPosts == null ? 0 : classPosts.size();
    }

    // 更新帖子数据的方法
    @SuppressLint("NotifyDataSetChanged")
    public void updatePosts(List<ClassPost> newClassPosts) {
        if (newClassPosts != null) {
            this.classPosts = newClassPosts;
            notifyDataSetChanged(); // 通知数据集改变
        }
    }

    // ViewHolder内部类
    public class PostViewHolder extends RecyclerView.ViewHolder {
        TextView textViewTitle;
        ImageView imagePostContent;
        TextView textDelete;
        private LinearLayout linearLayout;
        // 其他视图组件...




        public PostViewHolder(View itemView) {
            super(itemView);
            // 绑定视图组件
            textViewTitle = itemView.findViewById(R.id.text_view_post_title);
            imagePostContent = itemView.findViewById(R.id.image_view_post_image);
            textDelete = itemView.findViewById(R.id.text_delete);
            linearLayout = itemView.findViewById(R.id.class_post);
            // 其他视图组件初始化...


            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 点击事件，跳转到NewsDetailActivity查看详情
                    String newsTitle = textViewTitle.getText().toString();
                    Intent intent = new Intent(context, NewsDetailActivity.class);
                    intent.putExtra("NEWS_TITLE", newsTitle); // newsItemTitle是点击新闻的标题
                    context.startActivity(intent);
                }
            });
            textDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 删除帖子的逻辑
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        ClassPost classPostToDelete = classPosts.get(position);
                        ClassPostDatabase post_db = ClassPostDatabase.getInstance(context);
                        post_db.deletePost(classPostToDelete.getId());

                        // 从列表中移除帖子，并更新UI
                        classPosts.remove(position);
                        notifyItemRemoved(position);
                    }
                }
            });

        }

        // bind方法用于将帖子数据绑定到ViewHolder的视图上
        public void bind(ClassPost classPost) {
            textViewTitle.setText(classPost.getTitle());
            if (classPost.hasImage()) {
                imagePostContent.setVisibility(View.VISIBLE);
                Glide.with(context)
                        .load(classPost.getImageUri())
                        .into(imagePostContent);
            } else {
//                imagePostContent.setVisibility(View.GONE);
            }
            // 绑定其他视图组件...
        }
    }
}